package com.gwsdk.paysdk.acitvityV2.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.gwsdk.paysdk.bean.HelperBean;
import com.gwsdk.paysdk.common.Constant;
import com.gwsdk.paysdk.http.process.HelperListProcess;
import com.gwsdk.paysdk.http.process.HelperProcess;
import com.gwsdk.paysdk.utils.GWInflaterUtils;
import com.gwsdk.paysdk.utils.TextUtils;
import com.gwsdk.paysdk.utils.ToastUtil;

public class GWCustomerServerFragment extends GWBaseFragment{

    private ImageView backItem;
    private HelperBean helperBean;
    private TextView qqTV;
    private TextView weChatTV;
    private TextView emailTV;
    private View qqView,wechatView,emailView,loadingView,contentView;

    @Override
    public int setContentView() {
        return GWInflaterUtils.getLayout(getActivity(),"gw_fragment_customer_server");
    }

    @Override
    public void initView(View view) {
        backItem = view.findViewById(GWInflaterUtils.getControl(getActivity(),"btn_back"));
        backItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        qqView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"qq_view"));
        wechatView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"wechat_view"));
        emailView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"email_view"));
        qqTV = view.findViewById(GWInflaterUtils.getControl(getActivity(),"qq_text"));
        weChatTV = view.findViewById(GWInflaterUtils.getControl(getActivity(),"wechat_text"));
        emailTV = view.findViewById(GWInflaterUtils.getControl(getActivity(),"email_text"));
        loadingView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"loading_view"));
        contentView = view.findViewById(GWInflaterUtils.getControl(getActivity(),"contentView"));
        view.findViewById(GWInflaterUtils.getControl(getActivity(),"copy_email")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                copyToClipboard(emailTV.getText().toString());
            }
        });

        view.findViewById(GWInflaterUtils.getControl(getActivity(),"copy_qq")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openQQ(qqTV.getText().toString());
            }
        });

        view.findViewById(GWInflaterUtils.getControl(getActivity(),"copy_wechat")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                copyToClipboard(weChatTV.getText().toString());
            }
        });
    }

    /**
     * 联系客服QQ
     * @param s
     */
    private void openQQ(String s) {
        if (checkApkExist(getActivity(), "com.tencent.mobileqq")){
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin="+s+"&version=1")));
            getActivity().finish();
        }else{
            ToastUtil.show(getActivity(),"本机未安装QQ应用");
        }
    }

    public boolean checkApkExist(Context context, String packageName) {
        if (packageName == null || "".equals(packageName))
            return false;
        try {
            ApplicationInfo info = context.getPackageManager().getApplicationInfo(packageName,
                    PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }

    private void copyToClipboard(String text) {
        ClipboardManager systemService = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
        systemService.setPrimaryClip(ClipData.newPlainText("text", text));
        ToastUtil.show(getActivity(),"复制完成");
    }

    @Override
    public void getData() {
        HelperProcess helperProcess = new HelperProcess();
        helperProcess.post(handler);
        HelperListProcess helperListProcess = new HelperListProcess();
        helperListProcess.post(handler);
    }

    Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            loadingView.setVisibility(View.GONE);
            contentView.setVisibility(View.VISIBLE);
            switch (msg.what){
                case Constant.HELPER_SUCCESS:
                    helperBean = (HelperBean) msg.obj;
                    if (TextUtils.isEmpty(helperBean.getAPP_QQ())){
                        qqView.setVisibility(View.GONE);
                    }else {
                        qqView.setVisibility(View.VISIBLE);
                        qqTV.setText(helperBean.getAPP_QQ());
                    }
                    if (TextUtils.isEmpty(helperBean.getAPP_QQ_GROUP())){
                        wechatView.setVisibility(View.GONE);
                    }else {
                        wechatView.setVisibility(View.VISIBLE);
                        weChatTV.setText(helperBean.getAPP_QQ_GROUP());
                    }
                    if (TextUtils.isEmpty(helperBean.getAPP_EMAIL())){
                        emailView.setVisibility(View.GONE);
                    }else {
                        emailView.setVisibility(View.VISIBLE);
                        emailTV.setText(helperBean.getAPP_EMAIL());
                    }
                    break;
                case Constant.HELPER_FAIL:
                    break;

            }
        }
    };
}
